package com.demo.myapplication.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.demo.myapplication.R;
import com.demo.myapplication.fragment.GridActionModeFragment;
import com.demo.myapplication.fragment.ListActionModeFragment;
import com.demo.myapplication.fragment.RecyclerActionModeFragment;

public class ActionModeActivity extends AppCompatActivity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_mode);

    initToolbar();
    ViewPager viewPager = (ViewPager) findViewById(R.id.container);
    PagerAdapter adapter = new SelectionViewPager(getSupportFragmentManager());
    viewPager.setAdapter(adapter);

    TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
  }

  private void initToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setTitle(getString(R.string.actionmode));
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        super.onBackPressed();
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private class SelectionViewPager extends FragmentStatePagerAdapter {
    public SelectionViewPager(FragmentManager fm) {
      super(fm);
    }

    @Override
    public Fragment getItem(int position) {
      Fragment fragment = null;
      switch (position) {
        case 0:
          fragment = GridActionModeFragment.newInstance(null);
          break;
        case 1:
          fragment = ListActionModeFragment.newInstance(null);
          break;
        case 2:
          fragment = RecyclerActionModeFragment.newInstance(null);
      }
      return fragment;
    }

    @Override
    public int getCount() {
      return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
      switch (position) {
        case 0:
          return getResources().getString(R.string.gridview);
        case 1:
          return getResources().getString(R.string.listview);
        case 2:
          return getResources().getString(R.string.recyclerview);
      }
      return null;
    }
  }
}
